////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek, 1999-2011.
//
// -------------------------------------------------------------------------
//  File name:   FlowMusicLogicTriggerNode.h
//  Version:     v1.00
//  Created:     27/05/2011 by Thomas Wollenzin
//  Description: This is the implementation of the FlowMusicLogicTriggerNode class
//
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "FlowBaseNode.h"

class CFlowNode_MusicLogicTrigger : public CFlowBaseNode<eNCT_Instanced>
{

public:

	//////////////////////////////////////////////////////////////////////////
	CFlowNode_MusicLogicTrigger(SActivationInfo* pActInfo)
	{
	}

	//////////////////////////////////////////////////////////////////////////
	~CFlowNode_MusicLogicTrigger()
	{
	}

	//////////////////////////////////////////////////////////////////////////
	IFlowNodePtr Clone(SActivationInfo* pActInfo)
	{
		return new CFlowNode_MusicLogicTrigger(pActInfo);
	}

	enum INPUTS 
	{
		eIN_ENABLE,
		eIN_DISABLE,
		eIN_CHECK
	};

	enum OUTPUTS 
	{
		eOUT_ENABLED,
		eOUT_DISABLED
	};

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void GetConfiguration(SFlowNodeConfig& config)
	{
		static const SInputPortConfig inputs[] = 
		{
			InputPortConfig_Void("Enable", _HELP("Enable the music logic system."), "Enable"),
			InputPortConfig_Void("Disable", _HELP("Disable the music logic system."), "Disable"),
			InputPortConfig_Void("Check", _HELP("Triggers the corresponding output port!"), "Check"),
			InputPortConfig_Null()
		};

		static const SOutputPortConfig outputs[] = 
		{
			OutputPortConfig_Void("Enabled", _HELP("Gets set once the music logic got enabled."), "Enabled"),
			OutputPortConfig_Void("Disabled", _HELP("Gets set once the music logic got disabled."), "Disabled"),
			OutputPortConfig_Null()
		};

		config.pInputPorts  = inputs;
		config.pOutputPorts = outputs;
		config.sDescription = _HELP("Music Logic Trigger");
		config.SetCategory(EFLN_APPROVED);
	}

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void Serialize(SActivationInfo* pActivationInfo, TSerialize ser)
	{
		ser.BeginGroup("LogicTrigger");
		ser.Value("m_bEnabled", m_bEnabled);
		ser.EndGroup();
	}

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void ProcessEvent(EFlowEvent event, SActivationInfo* pActInfo)
	{
		switch (event)
		{
		case eFE_Initialize:
			{
				EnableMusicLogic(m_bEnabled);
				ActivateOutput(pActInfo, m_bEnabled ? eOUT_ENABLED : eOUT_DISABLED, true);
			}
			break;
		case eFE_Activate:
			{
				if (IsPortActive(pActInfo, eIN_ENABLE))
				{
					if (EnableMusicLogic(true))
					{
						ActivateOutput(pActInfo, eOUT_ENABLED, true);
						m_bEnabled = true;
					}
				}
				else if (IsPortActive(pActInfo, eIN_DISABLE))
				{
					if (EnableMusicLogic(false))
					{
						ActivateOutput(pActInfo, eOUT_DISABLED, true);
						m_bEnabled = false;
					}
				}
				else if (IsPortActive(pActInfo, eIN_CHECK))
				{
					ActivateOutput(pActInfo, IsMusicLogicActive() ? eOUT_ENABLED : eOUT_DISABLED, true);
				}
			}
			break;
		}
	}

	//////////////////////////////////////////////////////////////////////////
	VIRTUAL void GetMemoryUsage(ICrySizer * s) const
	{
		s->Add(*this);
	}

	//////////////////////////////////////////////////////////////////////////
	inline IMusicLogic* const GetMusicLogic() const
	{
		if (gEnv->pGame)
		{
			IGameFramework const* const pGameFrameWork = gEnv->pGame->GetIGameFramework();

			if (pGameFrameWork)
			{
				return pGameFrameWork->GetMusicLogic();
			}
		}

		return NULL;
	}

	//////////////////////////////////////////////////////////////////////////
	inline bool const EnableMusicLogic(bool const bEnable) const
	{
		IMusicLogic* const pMusicLogic = GetMusicLogic();

		if (pMusicLogic)
		{
			return bEnable ? pMusicLogic->Start() : pMusicLogic->Stop();
		}

		return false;
	}

	//////////////////////////////////////////////////////////////////////////
	inline bool const IsMusicLogicActive() const
	{
		IMusicLogic* const pMusicLogic = GetMusicLogic();

		if (pMusicLogic)
		{
			return pMusicLogic->IsActive();
		}

		return false;
	}

private:

	static bool m_bEnabled;
};

bool CFlowNode_MusicLogicTrigger::m_bEnabled = false;

REGISTER_FLOW_NODE("Music:LogicTrigger", CFlowNode_MusicLogicTrigger);
